package com.rabbit.diary.bean.core;

import java.io.Serializable;

public class Result<T> implements Serializable{
	 private String code;
	    private String message;
	    private T data;

	    private Result(String code, String message, T data) {
	        this.code = code;
	        this.message = message;
	        this.data = data;
	    }

	    private Result(String code, String message) {
	        this.code = code;
	        this.message = message;
	        this.data = null;
	    }

	    /**
	     * 带数据成功返回
	     *
	     * @param data
	     * @param <T>
	     * @return
	     */
	    public static <T> Result<T> success(T data) {
	        return new Result<>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getDesc(), data);
	    }

	    /**
	     * 不带数据成功返回
	     *
	     * @return
	     */
	    public static <T> Result<T> success() {
	        return success(null);
	    }

	    /**
	     * 通用错误返回
	     *
	     * @param exceptionCodeEnum
	     * @return
	     */
	    public static <T> Result<T> error(ExceptionCodeEnum exceptionCodeEnum) {
	        return new Result<>(exceptionCodeEnum.getCode(), exceptionCodeEnum.getDesc());
	    }

	    /**
	     * 通用错误返回
	     *
	     * @param exceptionCodeEnum
	     * @param msg
	     * @return
	     */
	    public static <T> Result<T> error(ExceptionCodeEnum exceptionCodeEnum, String msg) {
	        return new Result<>(exceptionCodeEnum.getCode(), msg);
	    }

	    /**
	     * 通用错误返回
	     *
	     * @param exceptionCodeEnum
	     * @param data
	     * @param <T>
	     * @return
	     */
	    public static <T> Result<T> error(ExceptionCodeEnum exceptionCodeEnum, T data) {
	        return new Result<>(exceptionCodeEnum.getCode(), exceptionCodeEnum.getDesc(), data);
	    }

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public T getData() {
			return data;
		}

		public void setData(T data) {
			this.data = data;
		}

	    
}
